#include "Player.h"
#include "Collision.h"
#include <math.h>
using namespace std;
using namespace sf;
Player::Player(Vector2f position, float health, Vector2f size, SpriteEnum spriteTexture, SpriteEnum bulletSprite) :
Character(position, health, size,spriteTexture,bulletSprite)
{
	_sprite.setFillColor(Color::White);
	setSpeed(Vector2f(1.5, 1.5));
}


void Player::shoot()
{
	
	for (int i = 0; i != isShootingVector.size();i++)
	{
		
		
		if (isShootingVector[i] == true)
		{
			
			
				bulletManager.getBulletMap()[i + 1]->setIsShooting(true);
				if (bulletManager.getBulletMap()[i+1]->getIsShooting())
				{
					
					bulletManager.getBulletMap()[i + 1]->move(cursorOrientation, _position, _size);
					
					if (bulletManager.getBulletMap()[i+1]->getIsShooting() == false)
					{
						isShootingVector[i] = false;
					}

				}
			
		}
	}
		
			
		
		

	

}


void Player::setUp(float x)
{
	up = x;
}
void Player::setDown(float x)
{
	down = x;
}
void Player::setLeft(float x)
{
	left = x;	
}
void Player::setRight(float x)
{
	right = x;
}

void Player::setBoost(int i)
{
	boost = i;
	
	
	
}

void Player::setShoot(int i,bool x)
{
	isShootingVector[i] = x;
}



void Player::move()
{


	if (up)
	{
	
		//check if below speedMax
		if (playerSpeed < playerSpeedMax)
		{
			
			//speed up 			
			playerSpeed += speedAcceleration;
			//check if above speedMax 		
			if (playerSpeed > playerSpeedMax)
			{
				//reset to speedMax
				playerSpeed = playerSpeedMax;
			}
		}
	}

	if (down)
	{
		//check if below speedMaxReverse
		if (playerSpeed > speedMaxReverse)
		{
			//speed up (in reverse)
			playerSpeed -= speedAcceleration;
			//check if above speedMaxReverse
			if (playerSpeed < speedMaxReverse) {

				//reset to speedMaxReverse 		
				playerSpeed = speedMaxReverse;
			} 			
		} 			
	} 	
	if (left) 		
	{ 				//turn left 	
		steering -= steeringAcceleration; 		
		//check if above steeringMax 			
		if (steering > steeringMax)
				{
					//reset to steeringMax
					steering = steeringMax;
				}
			}

			if (right)
			{
				//turn right
				steering += steeringAcceleration;
				//check if above steeringMax
				if (steering < -steeringMax) 		
				{ 					//reset to steeringMax 		
					steering = -steeringMax; 			
				} 		
			} 			 	
			// friction	 		
			playerSpeed *= friction;
			// prevent drift 			
			if (playerSpeed > 0 && playerSpeed < 0.05)
			{ 			
				playerSpeed = 0;
			} 			 		
			// calculate velocity based on speed 		
			velocity.x = float(sin(orientation.x * Pi / 180) * playerSpeed);
			velocity.y = float(cos(orientation.x * Pi / 180) * -playerSpeed);
			// update position	 		
				 		
			// prevent steering drift (right) 	
			if(steering > 0)
					{
						// check if steering value is really low, set to 0
						if (steering < 0.05)
						{
							steering = 0;
						}
					}
					// prevent steering drift (left)
				else if (steering < 0) 			{ 		
					// check if steering value is really low, set to 0 		
					if(steering > -0.05)
					{
						steering = 0;
					}
				}
			
				// apply steering friction
				steering = steering * steeringFriction;

				// make car go straight after driver stops turning
				steering -= float((steering * 0.1));

				// rotate
				orientation.x += steering * playerSpeed;
				


	
		switch (mapCollision(_position,_size))
		{

			case COLLI_LEFT:
			{
			_position.x = _position.x + 1;
			velocity = Vector2f(0, 0);
			
			break; 
			}

			case COLLI_RIGHT:
			{
			_position.x = _position.x - 1; 
			velocity = Vector2f(0, 0);
			
			break; 
			}

			case COLLI_TOP:
			{
			_position.y = _position.y + 1;
			velocity = Vector2f(0, 0);
			
			break; 

			}

			case COLLI_BOTTOM:
			{
			_position.y = _position.y - 1;
			velocity = Vector2f(0, 0);
			
			break; 
			}

			default:
			{ 
					   _position.x += velocity.x;
					   _position.y += velocity.y;
			
			break; 
			}		


		
		

	}

		if (boost == 1)
		{
			

			if (boostReservoir >=1)
			{
				playerSpeedMax = 2;
				boostReservoir--;
			}
			else
			{
				boostReservoir = boostReservoir + float(0.01);
				playerSpeedMax = 1;


			}

		}
		else
		{
			
			if (boostReservoir <= maxBoostReservoir)
			{
				boostReservoir = boostReservoir + float(0.1);
			}
			playerSpeedMax = 1;
			

		}
	

	
}

void Player::stopAction()
{
	setUp(0);
	setDown(0);
	setLeft(0);
	setRight(0);
	setBoost(0);
}



void Player::characterInterpretEvent(EventEnum keyEvent)
{
	



	switch (keyEvent)
	{
		
		//direction press-------
		case UP_PRESS:
		{
			
			setUp(1);			
			break;
		}

		case DOWN_PRESS:
		{
			setDown(1);				
			break;
		}

		case LEFT_PRESS:
		{
			setLeft(1);			
			break;
		}

		case RIGHT_PRESS:
		{
			setRight(1);			
			break;
		}
		
	

		//direction release-------

		case UP_RELEASE:
		{			
			
			setUp(0);
			break;
		}

		case DOWN_RELEASE:
		{
			setDown(0);
			break;
		}

		case LEFT_RELEASE:
		{
			setLeft(0);
			break;
		}

		case RIGHT_RELEASE:
		{
			setRight(0);
			break;
		}


		//Action
		case FIRE_PRESS:
		{
			
			if (itShootVector == numberBullet)
			{
				itShootVector = 0;
			}
			setShoot(itShootVector, 1);
			itShootVector++;
			break;
		}

		case RIGHT_CLICK_PRESS:
		{	
					
		setBoost(1);		
		 break;
		}

		case RIGHT_CLICK_RELEASE:
		{	
									
			setBoost(0);
			
		 break;
		}

		case MILLISECOND_EVENT:
		{
		 
		}

		default:
		{
			break;
		}
		

		

	}


}


